package zhengyanze.com.aipoemmax.guide.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.activity.PoemDetilsActivity;
import zhengyanze.com.aipoemmax.guide.bean.PoemBean;

import static zhengyanze.com.aipoemmax.guide.activity.PoemDetilsActivity.POEM_KEY;

/**
 * 主页诗句Adapter
 * Created by MrZheng on 2017/7/20.
 */

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainViewHolder> {

    ArrayList<PoemBean> mPoemBeans;

    public MainAdapter(ArrayList<PoemBean> mPoemBeans) {
        this.mPoemBeans = mPoemBeans;
    }

    public class MainViewHolder extends RecyclerView.ViewHolder {
        TextView tv_poemt;
        TextView tv_poema;
        TextView tv_poemc;

        public MainViewHolder(View itemView) {
            super(itemView);
            tv_poemt = itemView.findViewById(R.id.tv_poemt);
            tv_poema = itemView.findViewById(R.id.tv_poema);
            tv_poemc = itemView.findViewById(R.id.tv_poemc);
        }
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MainViewHolder holder, int position) {
        final PoemBean bean = mPoemBeans.get(position);
        holder.tv_poemt.setText(bean.getTitle());
        holder.tv_poema.setText(bean.getAuthor());
        holder.tv_poemc.setText(bean.getContent().split("。")[0]);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(holder.itemView.getContext(), PoemDetilsActivity.class);
                intent.putExtra(POEM_KEY,bean);
                holder.itemView.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mPoemBeans.size();
    }
}
